package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.AllProject2Activity;
import com.wnhz.work.yz.activity.ProjectDetailsActivity;
import com.wnhz.work.yz.activity.RecordUsedListActivity;
import com.wnhz.work.yz.bean.AllProjectBean;
import com.wnhz.work.yz.bean.BudgetPayListBean;
import com.wnhz.work.yz.bean.ProjectChildBean;
import com.wnhz.work.yz.view.NoScrollListView;
import com.wnhz.work.yz.view.SkillBookPop;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2016/12/27.
 */
public class AllProject3Adapter extends BaseAdapter {
    private BaseActivity context;
    private List<AllProjectBean> beanList = new ArrayList<>();
    private View.OnClickListener listener;
    private ProjectContentAdapter mAdapter;
    private List<ProjectChildBean> dataList = new ArrayList<>();

    public AllProject3Adapter(Context context, List<AllProjectBean> beanList, View.OnClickListener listener) {
        try {
            this.context = (AllProject2Activity) context;
        } catch (Exception e) {
            this.context = (RecordUsedListActivity) context;
        }
        this.listener = listener;
        this.beanList = beanList;
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return beanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        AllProjectViewHolder viewHolder;
        if (convertView == null) {
            FindView findView = new FindView().invoke();
            convertView = findView.getConvertView();
            viewHolder = findView.getViewHolder();

            convertView.setTag(viewHolder);
        } else {
            try {
                viewHolder = (AllProjectViewHolder) convertView.getTag();
            } catch (Exception e) {
                FindView findView = new FindView().invoke();
                convertView = findView.getConvertView();
                viewHolder = findView.getViewHolder();
            }
        }
        viewHolder.ll_click.requestFocus();
        viewHolder.order_time.setText("下单时间：" + beanList.get(position).getOrder_time());
        viewHolder.tv_type.setTextColor(0xffe9573d);
        viewHolder.rl_type1.setVisibility(View.GONE);
        viewHolder.rl_type2.setVisibility(View.GONE);
        try {
            viewHolder.tv_type.setText(beanList.get(position).getStatus_name());
        } catch (Exception e) {
            return convertView;
        }
        String str = "项目名称:<font color=\"#e9573d\">" + beanList.get(position).getRecord_name() + "</font>";
        viewHolder.xiangmu_name.setText(Html.fromHtml(str));

        viewHolder.tv_project_code.setText("合同号:" + beanList.get(position).getBudget_no());

        viewHolder.tv_goods_number.setText("共" + beanList.get(position).getCount() + "套商品 合计：¥");
        viewHolder.tv_all_price.setText(beanList.get(position).getTotal_fee());

        dataList = beanList.get(position).getData();

        mAdapter = new ProjectContentAdapter(context, dataList, beanList.get(position).getBudget_id(), beanList.get(position).getCan_pay() + "", beanList.get(position).getRecord_name());
        viewHolder.my_listView.setAdapter(mAdapter);

        viewHolder.tv_add_goods.setOnClickListener(listener);
        viewHolder.tv_add_goods.setTag(position);
        viewHolder.tv_schedule.setOnClickListener(listener);
        viewHolder.tv_schedule.setTag(position);
        viewHolder.tv_end_project.setOnClickListener(listener);
        viewHolder.tv_end_project.setTag(position);
        viewHolder.tv_plan.setOnClickListener(listener);
        viewHolder.tv_plan.setTag(position);
        viewHolder.tv_cancel.setOnClickListener(listener);
        viewHolder.tv_cancel.setTag(position);
        viewHolder.ll_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ProjectDetailsActivity.class);
                try {
                    intent.putExtra("tv_project_code", beanList.get(position).getRecord_name());
                    intent.putExtra("canPay", beanList.get(position).getCan_pay() + "");
                    intent.putExtra("budget_id", beanList.get(position).getBudget_id());
                    context.startActivity(intent);
                } catch (Exception e) {
                    notifyDataSetChanged();
                }
            }
        });
        viewHolder.goto_pay.setVisibility(View.GONE);

        return convertView;
    }

    private BudgetPayListBean.InfoEntity info;


    class AllProjectViewHolder {
        TextView tv_project_code;
        TextView tv_type;
        NoScrollListView my_listView;
        TextView tv_all_price;
        TextView tv_goods_number;
        TextView tv_add_goods;
        TextView tv_schedule;
        TextView tv_end_project;
        RelativeLayout rl_type1;
        RelativeLayout rl_type2;
        TextView tv_plan;
        TextView tv_cancel;
        LinearLayout ll_click;
        TextView xiangmu_name;
        TextView goto_pay;
        TextView order_time;
    }

    private class FindView {
        private View convertView;
        private AllProjectViewHolder viewHolder;

        public View getConvertView() {
            return convertView;
        }

        public AllProjectViewHolder getViewHolder() {
            return viewHolder;
        }

        public FindView invoke() {
            viewHolder = new AllProjectViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.all_project_item, null);
            viewHolder.tv_project_code = (TextView) convertView.findViewById(R.id.tv_project_code);
            viewHolder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            viewHolder.my_listView = (NoScrollListView) convertView.findViewById(R.id.my_listView);
            viewHolder.tv_all_price = (TextView) convertView.findViewById(R.id.tv_all_price);
            viewHolder.tv_goods_number = (TextView) convertView.findViewById(R.id.tv_goods_number);
            viewHolder.tv_add_goods = (TextView) convertView.findViewById(R.id.tv_add_goods);//新增商品
            viewHolder.tv_schedule = (TextView) convertView.findViewById(R.id.tv_schedule);//项目进度
            viewHolder.tv_end_project = (TextView) convertView.findViewById(R.id.tv_end_project);//终止项目
            viewHolder.rl_type1 = (RelativeLayout) convertView.findViewById(R.id.rl_type1);
            viewHolder.rl_type2 = (RelativeLayout) convertView.findViewById(R.id.rl_type2);
            viewHolder.tv_plan = (TextView) convertView.findViewById(R.id.tv_plan);//审核进度
            viewHolder.tv_cancel = (TextView) convertView.findViewById(R.id.tv_cancel);//取消订单
            viewHolder.ll_click = (LinearLayout) convertView.findViewById(R.id.ll_click);
            viewHolder.goto_pay = (TextView) convertView.findViewById(R.id.goto_pay);
            viewHolder.xiangmu_name = (TextView) convertView.findViewById(R.id.xiangmu_name);
            viewHolder.order_time = (TextView) convertView.findViewById(R.id.order_time);
            return this;
        }
    }

    private static List<String> imgUrl = new ArrayList<>();
    private WindowManager.LayoutParams lp;
    private SkillBookPop menuWindow;
    private final int FROM_CAMERA_CODE = 111;// 调用相机更改背景图片的请求code
    private final int FROM_ALBUM_CODE = 112;// 调用相册更改背景图片的请求code

    public void showPopmenu(View v) {
        menuWindow = new SkillBookPop(context, menuItemsOnClick);
        menuWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            public void onDismiss() {
                WindowManager.LayoutParams lp = context.getWindow().getAttributes();
                lp.alpha = 1f;
                context.getWindow().setAttributes(lp);
            }
        });

//        // 显示窗口位置
        menuWindow.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        // 设置layout在PopupWindow中显示的位置
        lp = context.getWindow().getAttributes();
        lp.alpha = 0.6f;
        context.getWindow().setAttributes(lp);

    }

    private boolean hasSdcard() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 弹出pop的菜单点击事件
     */
    private View.OnClickListener menuItemsOnClick = new View.OnClickListener() {
        Intent intent = null;

        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.from_camera2:
                    String imageName = "";
                    System.out.println("=====1234=====");
                    intent = new Intent("android.media.action.IMAGE_CAPTURE");
                    // 判断存储卡是否可以用，可用进行存储
                    if (hasSdcard()) {
                        String time = System.currentTimeMillis() + "";
                        imageName = time + ".jpg";
                        SharedPreferences sp = context.getSharedPreferences("imageName.txt", Context.MODE_PRIVATE);
                        sp.edit().putString("imageName", imageName).commit();

                        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/youZhu/", imageName)));
                    }
                    System.out.println("拍照的路径---" + imageName);
                    context.startActivityForResult(intent, FROM_CAMERA_CODE);

                    break;
                case R.id.from_album2:

                    Intent intent = new Intent(Intent.ACTION_PICK,
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    context.startActivityForResult(intent, FROM_ALBUM_CODE);

                    break;

                default:
                    break;
            }
        }
    };

}
